package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.Apometre;
import com.googlecode.jmicroerp.model.Magazin;
import com.googlecode.jmicroerp.model.Produs;
import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.repository.GenericProperty;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import java.util.Date;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Ionel.David
 */
public class EditareApometrePage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<Apometre> apometreService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> magazinService;
    private Apometre apometre;
    private Magazin magazin;
    
    public EditareApometrePage(Long idApometre) {
        apometre = apometreService.getById(Apometre.class, idApometre);
        initPage();
    }
    
    public EditareApometrePage() {
        apometre = new Apometre();
        initPage();
    }
    
    private void initPage() {
        Form<Apometre> form = new Form<Apometre>("form");
        form.setModel(new CompoundPropertyModel<Apometre>(apometre));
        
        final DropDownChoice<Magazin> ddcMagazin = new DropDownChoice<Magazin>("magazin", new PropertyModel<Magazin>(this, "magazin"),
                new LoadableDetachableModel<List<Magazin>>() {

            @Override
            protected List<Magazin> load() {
                return magazinService.getList(Magazin.class, null, null);
            }
        }, new ChoiceRenderer<Magazin>("denumire", "id"));
        form.add(ddcMagazin);
        
        
        final DropDownChoice<Recipient> ddcRecipient = new DropDownChoice<Recipient>("recipient", new LoadableDetachableModel<List<Recipient>>() {

            @Override
            protected List<Recipient> load() {
                return recipientService.getList(Recipient.class, null, null);
            }
        }, new ChoiceRenderer<Recipient>("denumire", "id"));
        ddcRecipient.setOutputMarkupId(true);
        form.add(ddcRecipient);
        
        DateTextField dtfDataCitirii = new DateTextField("dataCitirii");
        dtfDataCitirii.add(new DatePicker());
        form.add(dtfDataCitirii);
        
        TextField<BigDecimal> tfCantitate = new TextField<BigDecimal>("valoare");
        tfCantitate.setRequired(true);
        form.add(tfCantitate);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (apometre.getId() == null) {
                    apometreService.insert(apometre);
                } else {
                    apometreService.update(apometre);
                }
                setResponsePage(VizualizareApometrePage.class);
            }
            
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareApometrePage.class);
            }
            
        });

        ddcMagazin.add(new AjaxFormComponentUpdatingBehavior("onclick") {
            
            @Override
            protected void onUpdate(AjaxRequestTarget art) {
                Magazin value = (Magazin) ddcMagazin.getDefaultModelObject();  
                
                if (value != null) {
                    List<GenericRestriction> restrictions = new ArrayList<GenericRestriction>();
                    GenericRestriction sameStoreRestriction = new GenericRestriction(new GenericProperty("magazin").setReference(new GenericProperty("id")), value.getId());
                    restrictions.add(sameStoreRestriction);
                    List<Recipient> recipiente = recipientService.getList(Recipient.class, restrictions, null);
                
                    ddcRecipient.setChoices(recipiente);
                    ddcRecipient.modelChanged();
                    art.add(ddcRecipient);
                }
            }
        });
        
        add(form);
    }
}
